package com.bdqn.config;

import com.bdqn.myInterceptor.AuthorizationInterceptor;
import com.bdqn.myInterceptor.ExceptionInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class myConfig implements WebMvcConfigurer {
    private static final String[] EXCLUDE=new String[]{
            "/ums/user/login",
            "/pms/product/**",
            "/pms/comment/commentList/**",
            "/error", //注意：controller入参报错，会重定向到容器 /error 路径处理
    };
    //把拦截器对象注入进来
    @Autowired
    private AuthorizationInterceptor authorizationInterceptor;
    @Autowired
    private ExceptionInterceptor exceptionInterceptor;
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        /**
         * allowedMethods 允许的请求方式 默认支持post和get以及 headers 应支持全部请求方式
         */
        registry.addMapping("/**").allowedOrigins("*").allowedMethods("*").allowedHeaders("*").maxAge(1800);
    }
    //注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
            //退出时候也要拦截 判断token  只有登录了才能退出
        registry.addInterceptor(authorizationInterceptor).addPathPatterns("/**").excludePathPatterns(EXCLUDE);
        /**
         * 对异常进行全部拦截
         */
        registry.addInterceptor(exceptionInterceptor).addPathPatterns("/**");
    }
}
